﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class bzjdControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            Bind();
        }
        protected void Go_Search(object sender, EventArgs e)
        {
            Bind();
        }
        protected void Bind()
        {
            CustomGridView CustomGridList1 = (CustomGridView)FindControl("CustomGridList1");
            Button btnsearch = (Button)FindControl("btnsearch");
            btnsearch.Click += new EventHandler(Go_Search);
            DataTable dt = _EluZSDataProvider.GetProjectJD(GetSqlWhere());
            CustomGridList1.DataSource = dt;
            CustomGridList1.RecordCount = dt.Rows.Count;
            CustomGridList1.PageSize = 10;
            CustomGridList1.DataKeyNames = new string[] { "ID" };
            CustomGridList1.DataBind();
            CustomGridList1.OnLoadData += new EventHandler(CustomGridList1_OnLoadData);
        }
        protected void CustomGridList1_OnLoadData(object sender, EventArgs e)
        {
            Bind();
        }
        protected string GetSqlWhere() {
            string sqlWhere = string.Empty;
            TextBox xmmc = (TextBox)FindControl("xmmc");
            TextBox lxh = (TextBox)FindControl("lxh");
            TextBox xkzh = (TextBox)FindControl("xkzh");
            TextBox jsdw = (TextBox)FindControl("jsdw");
            if (!string.IsNullOrEmpty(xmmc.Text.Trim()))
            {
                sqlWhere += " and t1.项目名称  like '%" + xmmc.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(lxh.Text.Trim()))
            {
                sqlWhere += " and t1.立项号 like '%" + lxh.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(xkzh.Text.Trim()))
            {
                sqlWhere += " and t.征收许可证号 like '%" + xkzh.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(jsdw.Text.Trim()))
            {
                sqlWhere += " and t1.建设单位 like '%" + jsdw.Text.Trim() + "%'";
            }
            if (_EluZSDataProvider.IsQZSBM(userid))
            {
//                sqlWhere += @" and (Select wm_concat(tt.ROLENAME)  From zs_USER_IN_ROLE t Join  zs_Role tt  On  t.roleid=tt.id
//Where  t.userid Like '%" + userid + "%') Like '%'||substr(t1.区属,0,2)||'%' ";
                sqlWhere += @" and (Select max(Rolename) Rolename FROM (SELECT sys_connect_by_path(Rolename,',') Rolename FROM (SELECT Rolename,rn rchild,(rn-1) rfather FROM (SELECT   t.Rolename,row_number() over (ORDER BY t.Rolename) rn FROM (Select Tt.Rolename From Zs_User_In_Role t Join Zs_Role Tt On t.Roleid = Tt.Id Where t.Userid Like '%" + userid + "%') t)) CONNECT BY PRIOR rchild=rfather START WITH rfather LIKE '%0')) Like '%' || substr(T1.区属,0,2) || '%'";
            }
            sqlWhere += " order by addtime desc";
            return sqlWhere;
        }
    }
}


